/**
 * Copyright (c) 2009-2011, chunquedong(YangJiandong)
 * 
 * This file is part of ChunMap project
 * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE(Version >=3)
 * 
 * History:
 *     2010-05-05  Jed Young  Creation
 */
package chunmap.model.relate.relateop;

import chunmap.model.geom.Geometry;
import chunmap.model.geom.LineString;
import chunmap.model.geom.Ring;
import chunmap.model.geom.Polygon;
import chunmap.model.relate.ComputeIm;
import chunmap.model.relate.IntersectionMatrix;
import static chunmap.model.relate.IntersectionMatrix.*;

/**
 * @author chunquedong
 * 
 */
public class LineString_Polygon extends ComputeIm {

	public LineString_Polygon(LineString l1, Polygon a2) {
		g1 = l1;
		g2 = a2;
	}

	@Override
	protected int inner2innerDim(Geometry g1, Geometry g2) {
		LineString l1 = (LineString) g1;
		Polygon a2 = (Polygon) g2;

		ComputeIm l2r = new LineString_LinearRing(l1, a2.getShell());
		IntersectionMatrix tim = l2r.getIM();

		if (tim.get(Inner, Inner) == EmptyDim)
			return EmptyDim;

		if (a2.inHoles(l1)) {
			return EmptyDim;
		}

		return LineDim;
	}

	@Override
	protected boolean within(Geometry g1, Geometry g2) {
		if (im.get(Inner, Inner) == EmptyDim)
			return false;

		LineString l1 = (LineString) g1;
		Polygon a2 = (Polygon) g2;

		if (!a2.getShell().containLineStringIn(l1)) {
			return false;
		}

		for (Ring r : a2.getHoles()) {
			ComputeIm l2r = new LineString_LinearRing(l1, r);
			IntersectionMatrix tim = l2r.getIM();
			if (tim.get(Inner, Inner) != EmptyDim) {
				return false;
			}
		}
		return true;
	}
}